package com.lc.recovery.ui.activity.businesshome;

import android.os.Bundle;
import android.text.Html;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.lc.recovery.R;
import com.lc.recovery.app.AppActivity;
import com.lc.recovery.http.glide.GlideApp;
import com.lc.recovery.http.request.ShopsInfoBusiness2Api;
import com.lc.recovery.http.request.ShopsInfoBusinessApi;
import com.lc.recovery.http.response.ShopsInfoBusiness2Bean;
import com.lc.recovery.http.response.ShopsInfoBusinessBean;
import com.lc.recovery.http.response.ShopsTopBean;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.core.widget.NestedScrollView;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;

public class ClassificationGuidelinesActivity extends AppActivity {
    @BindView(R.id.rv_businessshop)
    RecyclerView rvBusinessshop;
    @BindView(R.id.img_banner)
    ImageView imgBanner;
    @BindView(R.id.rv_guidelines)
    RecyclerView rvGuidelines;
    @BindView(R.id.tv_class)
    TextView tvClass;
    @BindView(R.id.tv_class_content)
    TextView tvClassContent;
    @BindView(R.id.tv_jinrizhidao)
    TextView tvJinrizhidao;
    @BindView(R.id.nestedscrollview)
    NestedScrollView nestedscrollview;
    private String pid;
    List<ShopsTopBean.DataBean> datas;
    private List<String> dataBeanList = new ArrayList<>();
    private BaseQuickAdapter<ShopsTopBean.DataBean, BaseViewHolder> businessshopadapter;

    private List<ShopsInfoBusinessBean.SonBean> guidelinesBeanList = new ArrayList<>();
    private BaseQuickAdapter<ShopsInfoBusinessBean.SonBean, BaseViewHolder> guidelinesadapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_classificationguidelines;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        pid = getIntent().getStringExtra("id");
        datas = (List<ShopsTopBean.DataBean>) getIntent().getSerializableExtra("datas");
        shopsInfo(pid);
        shopsInfo2(pid);
        businessshopadapter = new BaseQuickAdapter<ShopsTopBean.DataBean, BaseViewHolder>
                (R.layout.item_recovery_shop, datas) {

            @Override
            protected void convert(BaseViewHolder baseViewHolder, ShopsTopBean.DataBean dataBean) {
                baseViewHolder.setText(R.id.tv_title, dataBean.getTitle());
                baseViewHolder.setText(R.id.tv_content, dataBean.getDescript());
                GlideApp.with(ClassificationGuidelinesActivity.this).load(dataBean.getPicurl()).transform(new RoundedCorners((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                        5, getResources().getDisplayMetrics())))
                        .apply(new RequestOptions().placeholder(R.mipmap.bg_01).error(R.mipmap.bg_01)).into(((ImageView) baseViewHolder.getView(R.id.img_bg)));
            }
        };

        businessshopadapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull @NotNull BaseQuickAdapter<?, ?> adapter, @NonNull @NotNull View view, int position) {

                shopsInfo(datas.get(position).getId() + "");
                shopsInfo2(datas.get(position).getId() + "");
            }
        });
        rvBusinessshop.setAdapter(businessshopadapter);


        guidelinesadapter = new BaseQuickAdapter<ShopsInfoBusinessBean.SonBean, BaseViewHolder>(R.layout.item_business_class, guidelinesBeanList) {
            @Override
            protected void convert(@NotNull BaseViewHolder baseViewHolder, ShopsInfoBusinessBean.SonBean bean) {
                baseViewHolder.setText(R.id.tv_name, bean.getTitle());
                baseViewHolder.setText(R.id.tv_content, bean.getDescript());
                baseViewHolder.setText(R.id.tv_money, bean.getPrice());
            }
        };
        guidelinesadapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull @NotNull BaseQuickAdapter<?, ?> adapter, @NonNull @NotNull View view, int position) {
//                startActivity(AlreadyWarehousingDetailsActivity.class);
            }
        });
        rvGuidelines.setAdapter(guidelinesadapter);
    }

    private void shopsInfo2(String pid) {

        EasyHttp.post(this)
                .api(new ShopsInfoBusiness2Api().setId(pid)).request(new HttpCallback<ShopsInfoBusiness2Bean>(this) {

            @Override
            public void onSucceed(ShopsInfoBusiness2Bean data) {
                if (data.getCode() == 200) {
                    tvClass.setText(data.getData().getTitle());
                    tvClassContent.setText(Html.fromHtml(data.getData().getContent()));
                } else {
                    toast(data.getMsg());
                }


            }


            @Override
            public void onFail(Exception e) {
//                super.onFail(e);

            }
        });
    }

    private void shopsInfo(String pid) {
        EasyHttp.post(this)
                .api(new ShopsInfoBusinessApi().setId(pid)).request(new HttpCallback<ShopsInfoBusinessBean>(this) {

            @Override
            public void onSucceed(ShopsInfoBusinessBean data) {
                if (data.getCode() == 200) {
                    if (!data.getData().getBanner().isEmpty()) {
                        GlideApp.with(ClassificationGuidelinesActivity.this).load(data.getData().getBanner()).transform(new RoundedCorners((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                                5, getResources().getDisplayMetrics())))
                                .apply(new RequestOptions().placeholder(R.mipmap.bg_01).error(R.mipmap.bg_01)).into(((ImageView) imgBanner));
                        imgBanner.setVisibility(View.VISIBLE);

                    } else {
                        imgBanner.setVisibility(View.GONE);
                    }

                    guidelinesBeanList.clear();
                    guidelinesBeanList.addAll(data.getSon());
                    guidelinesadapter.setNewData(guidelinesBeanList);
                    guidelinesadapter.notifyDataSetChanged();

                    if (data.getSon().size() > 0) {
                        tvJinrizhidao.setVisibility(View.VISIBLE);
                    } else {
                        tvJinrizhidao.setVisibility(View.GONE);
                    }
                } else {
                    toast(data.getMsg());
                }


            }


            @Override
            public void onFail(Exception e) {
//                super.onFail(e);

            }
        });
    }


}

